package com.dinggou.controller;

import com.dinggou.common.ResponseCode;
import com.dinggou.common.ServerResponse;
import com.dinggou.entity.Department;
import com.dinggou.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Resource(name="departmentServiceImpl")  //这个注解表示自动注入adminSeerviceImpl业务实现类
    @Autowired      //该注解表示后紧跟的变量进行封装，即自动产生getter和setter
    private DepartmentService departmentService;


    /*跳转到管理员的list*/
    @RequestMapping("/departmentlist")
    public String departmentlist(){
        return "departmentlist";
    }

    /****
     * 部门删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ServerResponse deleteByPrimaryKey(Integer id){
        if(departmentService.deleteByPrimaryKey(id)>0){
            return ServerResponse.createBySuccess(1,"删除管部门单位成功");
        }else{
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"删除数据失败");
        }
    }

    /****
     * 添加部门单位
     * @param record
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public ServerResponse insert(Department record){

        Department department=new Department();
        department.setName(record.getName());
        department.setdManager(record.getdManager());

        if(departmentService.insert(department)>0){
            return ServerResponse.createBySuccess("添加数据成功",department);
        }else{
            return ServerResponse.createByError();
        }
    }


    /****
     * 多个添加部门单位
     * @param record
     * @return
     */
    @RequestMapping("/insertSelective")
    @ResponseBody
    public ServerResponse insertSelective(Department record){
        Department department=new Department();
        department.setName(record.getName());
        department.setdManager(record.getdManager());


        if(departmentService.insert(department)>0){
            return ServerResponse.createBySuccess("添加数据成功",department);
        }else{
            return ServerResponse.createByError();
        }
    }

    /****
     * 通过id查询
     * @param id
     * @return
     */
    @RequestMapping("/selectByPrimaryKey")
    @ResponseBody
    public ServerResponse selectByPrimaryKey(Integer id){
        Department department =departmentService.selectByPrimaryKey(id);

        if(department != null){
            return ServerResponse.createBySuccess(1,department);
        }else{
            return ServerResponse.createByErrorMessage("找不到管理员");
        }
    }


    @RequestMapping("/dep_listall")
    @ResponseBody
    public ServerResponse listall(HttpServletRequest request, HttpServletResponse response){

        List<Department> departmentList=departmentService.selectAll();
        if(departmentList.size()>0){
            return ServerResponse.createBySuccess(departmentList.size(),departmentList);
        }else{
            return ServerResponse.createByErrorMessage("找不到部门单位信息");
        }
    }

    @RequestMapping("/updateByPrimaryKeySelective")
    @ResponseBody
    public ServerResponse updateByPrimaryKeySelective(Department record){
        Department department=new Department();
        department.setId(record.getId());
        department.setName(record.getName());
        department.setdManager(record.getdManager());

        if(departmentService.updateByPrimaryKeySelective(department)>0){
            return ServerResponse.createBySuccess("更新部门单位数据成功",department);
        }else{
            return ServerResponse.createByErrorMessage("更新部门单位数据失败");
        }
    }


    /****
     * updateByPrimaryKey通过id更新
     * @param record
     * @return
     */
    @RequestMapping("/updateByPrimaryKey")
    @ResponseBody
    public ServerResponse updateByPrimaryKey(Department record){
        Department department=new Department();
        department.setId(record.getId());
        department.setName(record.getName());
        department.setdManager(record.getdManager());

        if(departmentService.updateByPrimaryKey(department)>0){
            return ServerResponse.createBySuccess("更新数据成功",department);
        }else{
            return ServerResponse.createByErrorMessage("更新部门单位数据失败");
        }
    }
}